package com.avic.rong.wedgit.treeRvAdapter;

import android.content.Context;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.goodsSort.activity.secondListActivity.GoodsSortBzActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Creater: Created by wangyz on 2019/8/29.
 * Email: 936804097@qq.com
 */
public class FilterAdapter extends AbsTreeListAdapter<FilterDao, OptionDao> {

    private List<TreeListDao<FilterDao, OptionDao>> selectList = new ArrayList<>();
    private List<TreeListDao<FilterDao, OptionDao>> treeList = new ArrayList<>();
    private Context mContext;

    public FilterAdapter(Context context, List<TreeListDao<FilterDao, OptionDao>> treeList) {
        super(treeList);
        this.mContext = context;
        this.treeList = treeList;
    }

    @Override
    public int groupLayoutId() {
        return R.layout.item_tree_title_layout;
    }

    @Override
    public int subLayoutId() {
        return R.layout.item_tree_child_layout;
    }

    @Override
    public void onBindGroupHolder(GroupItemViewHolder holder, FilterDao filterDaoTwo, int groupIndex, int position) {

        holder.itemView.setOnClickListener(new MyGroupOnClickListener(groupIndex));

        TextView mTvFilter = holder.itemView.findViewById(R.id.mTvFilter);
        mTvFilter.setText(filterDaoTwo.getmTitle() == null ? "" : filterDaoTwo.getmTitle());

        CheckBox mCbDesc = holder.itemView.findViewById(R.id.mCbDesc);

        boolean isCheack = getmGroupItemStatus().get(groupIndex);
        mCbDesc.setChecked(isCheack);

        //选中的显示在右侧
//        String selectedOptions = "";
//        TreeListDao<FilterDao, OptionDao> treeListDao = treeList.get(groupIndex);
//        List<OptionDao> subList = treeListDao.getSubList();
//        for (OptionDao temp : subList) {
//            if (temp.isCheck()) {
//                selectedOptions = selectedOptions + temp.getmTitle() + ",";
//            }
//        }
//        if (selectedOptions != null && selectedOptions != "") {
//            mCbDesc.setText(selectedOptions);
//        } else {
//            mCbDesc.setText("");
//        }
    }


    class MyGroupOnClickListener implements View.OnClickListener {

        int groupIndex;
        public MyGroupOnClickListener(int groupIndex) {
            this.groupIndex = groupIndex;
        }

        @Override
        public void onClick(View v) {
            toggleMoreHide(groupIndex);
        }
    }


    @Override
    public void onBindSubHolder(SubItemViewHolder holder, OptionDao optionDaoTwo, int subIndex, int groupIndex, int position) {
        if (holder == null) return;
        CheckBox mCbSub = holder.itemView.findViewById(R.id.mCbSub);
        mCbSub.setText(optionDaoTwo.getmTitle());
//        mCbSub.setChecked(optionDaoTwo.isCheck());
        mCbSub.setChecked(false);
        mCbSub.setOnClickListener(new MySubClickListener(mCbSub, groupIndex, optionDaoTwo));
    }


    class MySubClickListener implements View.OnClickListener {
        CheckBox mCbSub;
        int groupIndex;
        OptionDao optionDaoTwo;

        public MySubClickListener(CheckBox mCbSub, int groupIndex, OptionDao optionDaoTwo) {
            this.mCbSub = mCbSub;
            this.groupIndex = groupIndex;
            this.optionDaoTwo = optionDaoTwo;
        }

        @Override
        public void onClick(View v) {
            boolean check = mCbSub.isChecked();
            if (!treeList.get(groupIndex).getGroupDao().isMultiCheck()) {
                List<OptionDao> subList = treeList.get(groupIndex).getSubList();
                for (OptionDao daoTwo : subList) {
                    daoTwo.setCheck(false);
                }
            }
            optionDaoTwo.setCheck(check);
//            mContext.startActivity(GoodsSortGjActivity.getIntent(mContext,optionDaoTwo.getmTitle()));
            mContext.startActivity(GoodsSortBzActivity.getIntent(mContext,String.valueOf(optionDaoTwo.get_ID())));
            notifyDataSetChanged();
        }
    }


    public List<TreeListDao<FilterDao, OptionDao>> getSelectOptionDao() {
        selectList.clear();
        for (TreeListDao<FilterDao, OptionDao> treeListDao : treeList) {
            List<OptionDao> list = new ArrayList<>();
            for (OptionDao daoTwo : treeListDao.getSubList()) {
                if (daoTwo.isCheck()) {
                    list.add(daoTwo);
                }
            }
            if (list.size() > 0) {
                treeListDao.getGroupDao().setmCheckOption(list);
                selectList.add(treeListDao);
            }
        }
        return this.selectList;
    }

}
